package ca.uguu.gamelib.libgdx.concepts;

import java.util.Observable;
import java.util.Observer;

import ca.uguu.gamelib.libgdx.globals.Globals;
import ca.uguu.gamelib.libgdx.globals.Android;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.utils.Disposable;

public abstract class UnmanagedTextureEntity implements Disposable, Observer{
	private boolean disposed = false;
	
	protected Texture texture;
	protected Sprite sprite;
	
	public UnmanagedTextureEntity(){
		Globals.ANDROID.addObserver(this);
	}
	
	/* Needs to be invoked every time the OpenGL context is lost, but the previous texture data doesn't need to be freed.
	 * Some other objects, notably those that do not need to be drawn dynamically, do not need to be prepared, because
	 * libGDX will reload static textures.
	 */
	protected abstract void assembleSprite();
	public void reloadSprite(){
		this.texture.dispose();
		this.assembleSprite();
	}
	
	@Override
	public void finalize() throws Throwable{
		if(!this.disposed){
			this.dispose();
		}
		super.finalize();
	}
	
	//Disposable
	@Override
	public void dispose(){
		this.texture.dispose();
		Globals.ANDROID.deleteObserver(this);
		this.disposed = true;
	}
	
	//Observer
	@Override
	public void update(Observable o, Object obj){
		if(o == Globals.ANDROID){
			switch((Android.AndroidStates)obj){
				case RESUMED:{
					this.assembleSprite();
					break;
				}
			}
		}
	}
}
